package testcase.app;

import org.testng.annotations.Test;

import api.app.SoftwareUpdate;
import common.Download;
import common.Host;
import common.MD5;
import common.TestData;

import java.io.File;

import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.DataProvider;

/**
 * 实现帮忙医APP版本更新检查SoftwareUpdate接口测试用例
 * @author maojj
 *
 */
public class TestSoftwareUpdate
{
	/**
	 * 帮忙医APP版本更新检查SoftwareUpdate接口测试环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	@DataProvider (name = "Test")
	public static String[][] testSoftwareUpdateDataProvider() throws Exception
	{
		Host.setup("Test");
		TestData td = new TestData("app/TestSoftwareUpdate.xml");
		return td.getParam();	
	}
	
	/**
	 * 帮忙医APP版本更新检查SoftwareUpdate接口生产环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	@DataProvider (name = "Prod")
	public static String[][] prodSoftwareUpdateDataProvider() throws Exception
	{
		Host.setup("Prod");
		TestData td = new TestData("app/TestSoftwareUpdate.xml");
		return td.getParam();	
	}
	
	/**
	 * 帮忙医APP版本更新检查SoftwareUpdate接口测试环境用例
	 * @param caseName - 测试用例名称
	 * @param channel - 渠道号
	 * @throws Exception
	 */
	@Test (dataProvider = "Test", groups = {"Test"})
	public void testSoftwareUpdateTestCase(String caseName, String channel) throws Exception
	{
		String result = SoftwareUpdate.get(channel);

		JSONObject jsonResult;
		if (result==null || result.isEmpty())
		{
			jsonResult = null;
		}
		else
		{
			jsonResult = new JSONObject(result);
		}

		// 检查点：接口返回Status是否为Success
		String checkpointStatus = jsonResult.getJSONObject("Data").getString("Status");
		Assert.assertEquals(checkpointStatus, "Success");

		// 检查点：接口返LastestUrl是否不为空
		String checkpointLastestUrl = jsonResult.getJSONObject("Data").getString("LastestUrl");
		Assert.assertNotEquals(checkpointLastestUrl.length(), 0);

	}
	
	/**
	 * 帮忙医APP版本更新检查SoftwareUpdate接口生产环境用例
	 * @param caseName - 测试用例名称
	 * @param channel - 渠道号
	 * @throws Exception
	 */
	@Test (dataProvider = "Prod", groups = {"Prod"})
	public void prodSoftwareUpdateTestCase(String caseName, String channel) throws Exception
	{
		String result = SoftwareUpdate.get(channel);
		
		JSONObject jsonResult;
		if (result==null || result.isEmpty())
		{
			jsonResult = null;
		}
		else
		{
			jsonResult = new JSONObject(result);
		}
		
		//检查点：接口返Status是否为Success
		String checkpointStatus = jsonResult.getJSONObject("Data").getString("Status");
		Assert.assertEquals(checkpointStatus, "Success");
		
		//检查点：接口返LastestUrl是否不为空
		String checkpointLastestUrl = jsonResult.getJSONObject("Data").getString("LastestUrl");
		Assert.assertNotEquals(checkpointLastestUrl.length(), 0);
		
		//判断是否为web下载，如果是则不执行后面用例
		String downloadType = jsonResult.getJSONObject("Data").getString("downloadType");
		if  (!downloadType.equalsIgnoreCase("1"))
		{
			return;
		}
		
		//检查点：安装包是否下载成功
		String filePath = Download.downloadDir + File.separator + "apk";
		String fileName = channel+".apk";
		
		//文件下载失败，尝试5次
		int downloadCount = 0;
		boolean downloadSuccess = true;
		do {
			try {
				Download.downloadFromUrl(checkpointLastestUrl, filePath, fileName);
				downloadSuccess = true;
			} catch (Exception e) {
				downloadCount++;
				downloadSuccess = false;
				System.out.println("渠道号：" + channel);
				System.out.println("下载地址：" + checkpointLastestUrl);
				System.out.println("第" + downloadCount + "次下载失败。");
				(new File(filePath + File.separator + fileName)).delete();
			}
		} while (downloadCount<5 && !downloadSuccess);
		
		File file = new File(filePath + File.separator + fileName);
		Assert.assertEquals(file.exists(), true);
		
		//检查点：MD5是否校验通过
		String checkpoinMD5 = jsonResult.getJSONObject("Data").getString("compareStr").toLowerCase();
		String fileMD5 = MD5.getMD5(file).toLowerCase();
		Assert.assertEquals(fileMD5, checkpoinMD5);;
	}

}
